package com.itheima.sys.service;

import com.itheima.common.exception.NcException;
import com.itheima.common.exception.enums.ResponseEnum;
import com.itheima.common.util.JsonUtils;
import com.itheima.sys.util.SmsHelper;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.itheima.common.pojo.Constant.*;

@Component
public class SmsService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SmsHelper smsUtil;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 发送验证码
     * @param mobile 手机号
     * @param type 1：登录 2:注册 3：重置管理员 4：变更企业管理员
     */
    public void shendVerifyCode(String mobile, Integer type) {
        if(type.equals(2)){
            Boolean flag = sysUserService.checkExists(mobile);
            if(flag){
                throw new NcException(ResponseEnum.USER_MOBILE_EXISTS);
            }
        }else{
            //其他情况 手机号必须存在
            Boolean flag = sysUserService.checkExists(mobile);
            if(!flag){
                throw new NcException(ResponseEnum.USER_NOT_REGISTER);
            }
        }
        String prefix = this.getPrefix(type);
        String code = RandomStringUtils.randomNumeric(4);

        //验证码短信模板
        String templateCode = "SMS_173696218";
        Map<String, String> params = new HashMap<>();
        params.put("code", code);
        //smsUtil.sendSms(mobile, templateCode, JsonUtils.toString(params));
        System.out.println("验证码：" + code + "--" + mobile);
        redisTemplate.opsForValue().set(prefix + mobile, code, 5, TimeUnit.MINUTES);
    }


    /**
     * 根据type类型获取redis key 前缀值
     * @param type
     * @return
     */
    public static String getPrefix(Integer type){
        String prefix  = "";
        switch (type){
            case 1:
                prefix = SMS_LOGIN_KEY_PREFIX;
                break;
            case 2:
                prefix = SMS_REGISTER_KEY_PREFIX;
                break;
            case 3:
                prefix = SMS_RESET_KEY_PREFIX;
                break;
            case 4:
                prefix = SMS_CHANGE_MANAGER_KEY_PREFIX;
                break;
            default:
                throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        return prefix;
    }


    /**
     * 判断验证码是否正确
     * @param mobile 手机号
     * @param checkcode 用户填写验证码
     * @param type 验证码类型
     * @return
     */
    public Boolean verifyCheckcode(String mobile, String checkcode, Integer type) {
        Boolean flag =  false;
        String prefix = this.getPrefix(type);

        String realCheckcode = redisTemplate.opsForValue().get(prefix + mobile);
        if(StringUtils.isNotBlank(realCheckcode) && realCheckcode.equals(checkcode)){
            flag = true;
        }
        return flag;
    }
}
